package br.com.caelum.lojavirtual.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.caelum.lojavirtual.modelo.Cd;
import br.com.caelum.lojavirtual.modelo.Musica;
import br.com.caelum.lojavirtual.modelo.Venda;
import br.com.caelum.lojavirtual.util.HibernateUtil;

public class DaoFactory {

	private final Session session;
	private Transaction transaction;
	public DaoFactory() {
		session = HibernateUtil.getSession();
	}
	
	public void beginTransaction(){
		this.transaction = this.session.beginTransaction();
	}
	public void commit(){
		this.transaction.commit();
		this.transaction = null;
	}
	
	public boolean hasTransaction(){
		return this.transaction !=null;
	}
	public void roolback(){
		this.transaction.rollback();
		this.transaction = null;
	}
	public void close(){
		this.session.close();
	}
	public UsuarioDao getUsuarioDao(){
		return new UsuarioDao(this.session);
	}
	public Dao<Cd, Long> getCdDao(){
		return new Dao<Cd, Long>(this.session,Cd.class);
	}
	public Dao<Musica, Long> getMusicaDao(){
		return new Dao<Musica, Long>(this.session,Musica.class);
	}
	public Dao<Venda, Long> getVendaDao(){
		return new Dao<Venda, Long>(this.session,Venda.class);
	}
}
